package com.qf.sys.controller;

import com.qf.common.R;
import com.qf.entity.sys.SysUser;
import com.qf.sys.param.LoginParam;
import com.qf.sys.service.SysUserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * ==== 天道酬勤 ====
 *
 * @author douzhouzhou
 * @date 2025/2/26
 * <p>
 * ==== 行稳致远 进而有为 ====
 */
//后端处理端口不一致问题
@CrossOrigin
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @PostMapping("/login")
    public Object login(@RequestBody LoginParam loginParam, HttpSession session) {
         SysUser sysUser = sysUserService.login(loginParam);
         session.setAttribute("sysUser",sysUser);
         //生成token
        //本例使用uuid作为token
        String token = UUID.randomUUID().toString() ;
        //{code:2000,data:{"token":"15649816156854"}}
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
         return R.ok(map);
    }
    @GetMapping("/info")
    public R info(HttpSession session) {
        //获取session
        SysUser sysUser = (SysUser)session.getAttribute("sysUser");
        return R.ok(sysUser);
    }
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        session.invalidate();//销毁session
        return R.ok(null);
    }
}
